#coding:utf-8
import cProfile
import json
import os
import time
import urllib
import urllib2
import uuid
import thread

# roleId = '7c4ed14689c1425987ff962ab78f2150';
#
# for i in range(1311,1322):
#     print  "insert into sys_role_function_rel value ('"+str(uuid.uuid4()).replace("-","")+"','"+str(i)+"','"+roleId+"');"

import threadpool
import threadpool as threadpool


def threadWord(name,i):
    print name

def getId():
    pass

def postUrl(url,data,header):
    data = urllib.urlencode(data)
    print data
    req = urllib2.Request(url,data=data,headers=header)
    res = urllib2.urlopen(req)
    html = res.read()
    print html

def start(pageNo):
        print pageNo
        pageNo = str(pageNo)
        url = 'http://113.108.174.45/kb/searchDocList/4?isUsing=&keyFlag=1&keyWords=&matchFlags=0,1,2,3,4&pageNo='+pageNo+'&pageSize=20&questionTypeId=-1&robotFlag=1&usedFlag=2'
        send_headers = {
            'temp-id': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ5YW5nLmxpdXl1QHRycy5jb20uY24iLCJleHAiOjE1MzM1NTA4MzF9.EyP6LTgmAq7MbVZQJ7fmTPGAMY5IbI6pGaH_hs-bb_U'
        }
        req = urllib2.Request(url,headers=send_headers)
        r = urllib2.urlopen(req)
        html = r.read()

        fp = os.open('1.txt',os.O_RDWR|os.O_CREAT)
        os.write(fp,html)

        #解析json
        ret = json.loads(html)
        items = ret['items']
        docids = []
        # print ret
        # print items
        print len(items)
        doc = ''
        for item in items:
            docids.append(item['docId'])
            doc+=item['docId']+";"
        # print docids
        print len(docids)
        docStr = str(docids)
        docStr = docStr.replace(',',';').replace('[','').replace(']','')
        print docStr
        # print doc
        data = {'docIdStr':doc,'robotFlag':1}
    #批量驳回
        # postUrl('http://113.108.174.44/kb/delDoc/4',data,send_headers)
        #提交审批
        #postUrl('http://113.108.174.45/kb/submitDoc/4',data,send_headers)
        # 审批通过
        print 'doc:'+doc
        psotdata = {'docIdStr':doc,'robotFlag':1,'status':'0','remark':''}
        print psotdata
        postUrl('http://113.108.174.45/kb/examineDoc/4',psotdata,send_headers)


def addRobotDoc():
    heard = {
        'temp-id':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBlckBzb2JvdC5jb20iLCJleHAiOjE1MzE4MjAzNDZ9.P6cWnbUF81xnSb6IydZboJ3JA4y_FtMdJ3WKEiJWg5w'
    }
    url = 'http://114.215.164.66/kb/addRobotDoc/4'
    data = {'robotFlag': 1,
            'questionTitle': '测试004',
            'linkFlag': 1,
            'matchFlag': 0,
            'answerDesc': '<p>测试004</p>',
            'answerTxt': '测试004',
            'linkUpdateFlag': 0,
            'answerSummary': '测试004',
            'questionTypeId': '2fb1a10e84c143b4808df914feb0f583',
            'auditStatus': 1,
            'answerFlag': 1,
            'effectTime': '2018-07-13',
            'invalidTime': '2018-07-13',
            'usedFlag': 4,
            'docTag': '办事指南',
            'department':'研发',
            'area':'天河区',
            'source':'不知道'
    }

    postUrl(url,data,heard)
    pass

def selectAllList():
    url = 'http://114.215.164.66/kb/searchDocList/4?createName=%E6%89%80%E6%9C%89&isUsing=&keyFlag=1&keyWords=&matchFlags=0,1,2,3,4&pageNo=1&pageSize=15&questionTypeId=-1&robotFlag=1&updateName=%E6%89%80%E6%9C%89&usedFlag=1&source=%e4%b8%8d%e7%9f%a5'
    heard = {
        'temp-id':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBlckBzb2JvdC5jb20iLCJleHAiOjE1MzE4MjAzNDZ9.P6cWnbUF81xnSb6IydZboJ3JA4y_FtMdJ3WKEiJWg5w'
    }
    req = urllib2.Request(url,headers=heard)
    res = urllib2.urlopen(req)
    html = res.read()
    print html
    pass

if __name__=="__main__":
    # for i in range(50):
    #     thread.start_new_thread(threadWord,("Thread"+str(i),i,))
    # start = time.time()
    # time.sleep(2)
    # end = time.time()
    # print end-start
    # cProfile.run("threadWord()", filename="test.out")

    pool = threadpool.ThreadPool(1)  # 创建线程池
    requests = threadpool.makeRequests(start, range(10,200))  # 创建
    for req in requests:
        pool.putRequest(req)  # 将req放到线程池中
    pool.wait()

    # addRobotDoc()
    # selectAllList()

'''
    for i in range(1,410):
        print i
        pageSize = str(i*1000)
        url = 'http://113.108.174.44/kb/searchDocList/4?isUsing=&keyFlag=1&keyWords=&matchFlags=0,1,2,3,4&pageNo=1&pageSize=200&questionTypeId=-1&robotFlag=1&usedFlag=3'
        send_headers = {
            'temp-id': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJtZWluYUAxNjMuY29tIiwiZXhwIjoxNTMxNDcwODU3fQ.78EjpHmA_YkoaeljEQUypoXVfHbFFoMSYjKnKeGD_ic'
        }
        req = urllib2.Request(url,headers=send_headers)
        r = urllib2.urlopen(req)
        html = r.read()

        fp = os.open('1.txt',os.O_RDWR|os.O_CREAT)
        os.write(fp,html)

        #解析json
        ret = json.loads(html)
        items = ret['items']
        docids = []
        # print ret
        # print items
        print len(items)
        doc = ''
        for item in items:
            docids.append(item['docId'])
            doc+=item['docId']+";"
        # print docids
        print len(docids)
        docStr = str(docids)
        docStr = docStr.replace(',',';').replace('[','').replace(']','')
        # print docStr
        # print doc
        data = {'docIdStr':doc,'robotFlag':1}
    #批量驳回
        postUrl('http://113.108.174.44/kb/delDoc/4',data,send_headers)
    # postUrl('http://113.108.174.44/kb/submitDoc/4',data,send_headers)

'''


